package com.zongxingh.falcon.logger.writer.impl;

import com.zongxingh.falcon.logger.common.SimpleLog;
import com.zongxingh.falcon.logger.config.LoggerProperties;
import com.zongxingh.falcon.logger.constant.LoggerLevel;
import com.zongxingh.falcon.logger.constant.LoggerTopic;
import com.zongxingh.falcon.logger.utils.FalconUtil;
import com.zongxingh.falcon.logger.writer.AbstractLogWriter;
import com.zongxingh.falcon.mq.config.MqProperties;

/**
 * Introduction:  默认的日志发送端
 * Package Name:  com.zongxingh.falcon.logger.writer.impl
 * Project Name:  falcon-logger
 * Author:  zongxingh
 * Email： zongxingh@163.com
 * Tel: 157*****778
 * Create Time:  2020/10/25 19:23
 */
public class DefaultLogWriter extends AbstractLogWriter {

    public DefaultLogWriter() {
        super();
    }

    public DefaultLogWriter(Class<?> clazz, MqProperties mqProperties,LoggerProperties properties) {
        super(clazz,mqProperties,properties);
    }

    @Override
    public void trace(String logText) {
        sendLog(LoggerTopic.LOG_MSG_TOPIC_TRACE, LoggerLevel.TRACE, logText);
    }

    @Override
    public void debug(String logText) {
        sendLog(LoggerTopic.LOG_MSG_TOPIC_DEBUG, LoggerLevel.DEBUG, logText);
    }

    @Override
    public void info(String logText) {
        sendLog(LoggerTopic.LOG_MSG_TOPIC_INFO, LoggerLevel.INFO, logText);
    }

    @Override
    public void warn(String logText) {
        sendLog(LoggerTopic.LOG_MSG_TOPIC_WARN, LoggerLevel.WARN, logText);
    }

    @Override
    public void error(String logText) {
        sendLog(LoggerTopic.LOG_MSG_TOPIC_ERROR, LoggerLevel.ERROR, logText);
    }

    private void sendLog(String topic, LoggerLevel level, String logText) {
        // 只有当需要发送的日志级别 大于等于 用户设置的日志级别时才发送日志
        if (level.toInt() >= this.getAllowLevel().toInt()) {
            SimpleLog simpleLog = createLog(topic,level, logText);
            super.sendLogMsg(simpleLog);
        }
    }

    /**
     * 根据配置创建 logEvent
     * @param topic 日志发送到队列的topic
     * @param level 日志级别
     * @param logText 日志内容
     * @return
     */
    private SimpleLog createLog(String topic, LoggerLevel level, String logText) {
        SimpleLog simpleLog = new SimpleLog(topic,level, logText);
        LoggerProperties.Formatter formatter = this.getLoggerProperties().getFormatter();
        if (formatter.isLogLevel()) {
            simpleLog.setLevel(level);
        }else {
            simpleLog.setLevel(null);
        }
        if (formatter.isThreadName()){
            simpleLog.setThreadName(Thread.currentThread().getName());
            simpleLog.setThreadId(Thread.currentThread().getId());
        }
        if (null != formatter.getTimeStampFormatter() && "".equals(formatter.getTimeStampFormatter())){
            simpleLog.setTimeStamp(FalconUtil.getTimeStamp(formatter.getTimeStampFormatter()));
        }
        if (formatter.isHostIp()) {
            simpleLog.setHostIp(FalconUtil.getServerIp());
        }
        if (formatter.isServiceName()){
            simpleLog.setClassName(this.getClazz().getName());
        }
        return simpleLog;
    }
}
